import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import hide from '@dfeidao/atom-web/ui/hide';

import get from '@dfeidao/atom-web/local/get';
import refresh from '@dfeidao/atom-web/url/refresh';
import { productid } from '../../atom/config';

export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	hide(fd.data.node);
	// 试题编号
	const res = await nodejs<{ dt: { results: string[], length: number } }>('employee-examination/zj-000005/s001', {});
	const question_no = res.dt.results;
	// 试卷编号：
	const paper_number = get(fd, 'paper_number');
	// 所属试题编号：
	const question_number = get(fd, 'question_number');
	// 答案
	const answer = get(fd, 'answer') as Array<{}>;
	// 正确答案：
	const right_answer = get(fd, 'right_answer') as Array<{}>;
	// 总分
	const exam_scores = get(fd, 'exam_score') as number;
	// 顺序号：
	const fzno = get(fd, 'fzno') as Array<{}>;
	// 题目：
	const topic = get(fd, 'topic') as Array<{ topic: string, topic_type: string, option_a: string, option_b: string, option_c: string, option_d: string, fzno: string }>;
	// 选项
	const option_a = Array();
	const option_b = Array();
	const option_c = Array();
	const option_d = Array();
	// 题目类型：
	const topic_type = Array();
	for (let i = 0; i < topic.length; i++) {
		topic_type.push(topic[i].topic_type);
		option_a.push(topic[i].option_a);
		option_b.push(topic[i].option_b);
		option_c.push(topic[i].option_c);
		option_d.push(topic[i].option_d);
	}
	// 是否正确：
	const is_right = Array();
	const score = Array();
	let exam_score = 0;
	for (let i = 0; i < answer.length; i++) {
		if (answer[i] === right_answer[i]) {
			is_right.push(true);
			score.push(Number(exam_scores / 20));
			exam_score += (Number(exam_scores / 20));
		} else {
			is_right.push(false);
			score.push(0);
		}
	}
	// 每道题的分数
	console.log("每道题分数", score);
	// 考试编号：
	const exam_id = get(fd, 'exam_id');
	// 考生编号：
	const candidate_no = get(fd, 'candidate_no');
	// 当前时间:
	const dateee = (new Date()).valueOf();
	// 数据回滚
	const arr = Array();
	topic.map((n) => {
		for (let i = 0; i < is_right.length; i++) {
			arr[i] = {
				fzno: fzno[i], answer: answer[i], right_answer: right_answer[i],
				option_a: option_a[i], option_b: option_b[i], option_c: option_c[i], option_d: option_d[i],
				paper_number: paper_number[0], question_no: question_no[0], question_number: question_number[i],
				score: score[i], topic: topic[i].topic, topic_type: topic_type[i], is_right: is_right[i]
			};
		}
		return arr;
	});
	console.log("arr:", arr);

	for (let i = 0; i < arr.length; i++) {
		const fzno1 = arr[i].fzno;
		const answer1 = arr[i].answer;
		const right_answer1 = arr[i].right_answer;
		const option_a1 = arr[i].option_a;
		const option_b1 = arr[i].option_b;
		const option_c1 = arr[i].option_c;
		const option_d1 = arr[i].option_d;
		const paper_number1 = arr[i].paper_number;
		const question_no1 = arr[i].question_no;
		const question_number1 = arr[i].question_number;
		const score1 = arr[i].score;
		const topic1 = arr[i].topic;
		const topic_type1 = arr[i].topic_type;
		const is_right1 = arr[i].is_right;
		await nodejs<{ code: 0 | 1; result: string }>('employee-examination/zj-000005/s002', {
			filter: {
				productid,
				fzno: fzno1,
				answer: answer1,
				right_answer: right_answer1,
				option_a: option_a1,
				option_b: option_b1,
				option_c: option_c1,
				option_d: option_d1,
				paper_number: paper_number1,
				question_no: question_no1,
				question_number: question_number1,
				score: score1,
				topic: topic1,
				topic_type: topic_type1,
				is_right: is_right1
			}
		});
	}
	const res1 = await nodejs<{ code: 0 | 1; result: string }>('employee-examination/zj-000005/s003', {
		filter: {
			productid,
			exam_id,
			paper_number: paper_number[0],
			candidate_no,
			exam_score,
			exam_time: dateee
		}
	});
	if (res1.code === 1) {
		alert("考试结束");
		refresh();
	}
}
